package org.jeecgframework.web.service.impl;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.web.service.ZyWarrantyPeriodServiceI;
import org.jeecgframework.web.system.pojo.base.ZyWarrantyPeriodEntity;
import org.jeecgframework.web.util.ExcelUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("zyWarrantyPeriodService")
@Transactional
public class ZyWarrantyPeriodServiceImpl extends CommonServiceImpl implements ZyWarrantyPeriodServiceI {

	public <T> void saveOrUpdateForCp(T entity) {
		
		super.saveOrUpdate(entity);
	}
	
 	public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
		this.doDelSql((ZyWarrantyPeriodEntity)entity);
 	}
 	
 	public <T> Serializable save(T entity) {
 		Serializable t = super.save(entity);
 		//执行新增操作配置的sql增强
 		this.doAddSql((ZyWarrantyPeriodEntity)entity);
 		return t;
 	}
 	
 	public <T> void saveOrUpdate(T entity) {
 		super.saveOrUpdate(entity);
 		//执行更新操作配置的sql增强
 		this.doUpdateSql((ZyWarrantyPeriodEntity)entity);
 	}
 	
 	/**
	 * 默认按钮-sql增强-新增操作
	 * @param id
	 * @return
	 */
 	public boolean doAddSql(ZyWarrantyPeriodEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作
	 * @param id
	 * @return
	 */
 	public boolean doUpdateSql(ZyWarrantyPeriodEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作
	 * @param id
	 * @return
	 */
 	public boolean doDelSql(ZyWarrantyPeriodEntity t){
	 	return true;
 	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @return
	 */
 	public String replaceVal(String sql,ZyWarrantyPeriodEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{item}",String.valueOf(t.getItem()));
 		sql  = sql.replace("#{sn}",String.valueOf(t.getSn()));
 		sql  = sql.replace("#{item_desc}",String.valueOf(t.getItemDesc()));
 		sql  = sql.replace("#{alternate_serial_nr}",String.valueOf(t.getAlternateSerialNr()));
 		sql  = sql.replace("#{creation_date}",String.valueOf(t.getCreationDate()));
 		sql  = sql.replace("#{serialized_item_group}",String.valueOf(t.getSerializedItemGroup()));
 		sql  = sql.replace("#{sold_to_bp}",String.valueOf(t.getSoldToBp()));
 		sql  = sql.replace("#{sold_to_bp_desc}",String.valueOf(t.getSoldToBpDesc()));
 		sql  = sql.replace("#{sold_to_address}",String.valueOf(t.getSoldToAddress()));
 		sql  = sql.replace("#{sold_to_addr_country}",String.valueOf(t.getSoldToAddrCountry()));
 		sql  = sql.replace("#{sold_to_contact}",String.valueOf(t.getSoldToContact()));
 		sql  = sql.replace("#{warranty}",String.valueOf(t.getWarranty()));
 		sql  = sql.replace("#{installation_date}",String.valueOf(t.getInstallationDate()));
 		sql  = sql.replace("#{revision}",String.valueOf(t.getRevision()));
 		sql  = sql.replace("#{delivery_date}",String.valueOf(t.getDeliveryDate()));
 		sql  = sql.replace("#{sales_order}",String.valueOf(t.getSalesOrder()));
 		sql  = sql.replace("#{sales_order_line}",String.valueOf(t.getSalesOrderLine()));
 		sql  = sql.replace("#{cluster}",String.valueOf(t.getCluster()));
 		sql  = sql.replace("#{cluster_desc}",String.valueOf(t.getClusterDesc()));
 		sql  = sql.replace("#{top_item}",String.valueOf(t.getTopItem()));
 		sql  = sql.replace("#{top_serial_nr}",String.valueOf(t.getTopSerialNr()));
 		sql  = sql.replace("#{work_site_address}",String.valueOf(t.getWorkSiteAddress()));
 		sql  = sql.replace("#{lot}",String.valueOf(t.getLot()));
 		sql  = sql.replace("#{sls_comp}",String.valueOf(t.getSlsComp()));
 		sql  = sql.replace("#{sls_sold_to_bp}",String.valueOf(t.getSlsSoldToBp()));
 		sql  = sql.replace("#{sls_sold_to_bp_desc}",String.valueOf(t.getSlsSoldToBpDesc()));
 		sql  = sql.replace("#{sls_ship_to_country}",String.valueOf(t.getSlsShipToCountry()));
 		sql  = sql.replace("#{lob}",String.valueOf(t.getLob()));
 		sql  = sql.replace("#{end_of_warranty}",String.valueOf(t.getEndOfWarranty()));
 		sql  = sql.replace("#{service_contract}",String.valueOf(t.getServiceContract()));
 		sql  = sql.replace("#{service_contract_desc}",String.valueOf(t.getServiceContractDesc()));
 		sql  = sql.replace("#{start_contract_coverage}",String.valueOf(t.getStartContractCoverage()));
 		sql  = sql.replace("#{end_contract_coverage}",String.valueOf(t.getEndContractCoverage()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}
 	
 	@Override
	public List<String> parseExcel(HttpServletRequest req) {
		List<String> snList = new ArrayList<String>();
		try {
			String filePath = req.getParameter("filePath");
			List<Map<String, Object>> mapList = ExcelUtil.analyzeExcel(filePath, "1");
			for (int i = 1; i < mapList.size();i++) {
				Map<String, Object> map = mapList.get(i);
				snList.add(map.get("1").toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return snList;
	}
	
 	
	@Override
	public String checkSnRepeat(List<String> snList,int begin,int end) {
		System.out.println("beginR:" + begin);
		System.out.println("endR:" + end);
		for(int i = begin;i <= end;i++ ){
			for(int c = i + 1;c < snList.size();c++){
				if(snList.get(i).equals(snList.get(c))){
					return snList.get(i);
				}
			}
		}
		return null;
	}
 	
}